﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace BingImages
{
    class Copy
    {
        private Folder _folder;
        private themeForm _themeForm;
        private ProgressBar _pbPicture;

        public Copy(themeForm themeForm = null)
        {
            this._folder = new Folder();

            if (themeForm != null)
            {
                this._themeForm = themeForm;
                this._pbPicture = this._themeForm.getPbPicture();
            }
        }

        public void copyFiles(string rootFolderPath, string destFolderPath, string option)
        {
            if (this._folder.exist(rootFolderPath) && this._folder.exist(destFolderPath))
            {
                FileInfo[] rootFiles = null;
                FileInfo[] destFiles = null;
                string combinePath = "";

                rootFolderPath = Environment.ExpandEnvironmentVariables(rootFolderPath);
                rootFiles = this._folder.getFiles(new DirectoryInfo(rootFolderPath), option);

                destFolderPath = Environment.ExpandEnvironmentVariables(destFolderPath);
                destFiles = this._folder.getFiles(new DirectoryInfo(destFolderPath), option);                

                if(this._themeForm != null)
                {
                    this._pbPicture.Visible = true;
                    this._pbPicture.Maximum = rootFiles.Length * 2;
                }

                foreach (FileInfo file in rootFiles)
                {
                    if (!destFiles.Any(x => x.Name == file.Name))
                    {
                        combinePath = Path.Combine(destFolderPath, file.Name);

                        file.CopyTo(combinePath, true);

                        if(this._themeForm != null)
                        {
                            this._pbPicture.Value += 1;
                        }
                    }
                }
            }
        }

        public void copyFile(string rootFilePath, string destFolderPath)
        {
            if (this._folder.exist(destFolderPath))
            {
                destFolderPath = Environment.ExpandEnvironmentVariables(destFolderPath);

                string fileName = Path.GetFileName(rootFilePath);
                string combinePath = Path.Combine(destFolderPath, fileName);
                File.Copy(rootFilePath, combinePath, true);
            }
        }

        public void copyPicture(string rootFolderPath, string destFolderPath, string option, Size size)
        {
            if(this._folder.exist(rootFolderPath) && this._folder.exist(destFolderPath))
            {
                FileInfo[] rootFiles = null;
                FileStream fs = null;
                string combinePath = "";
                Image tempPicture = null;
                Image picture = null;

                rootFolderPath = Environment.ExpandEnvironmentVariables(rootFolderPath);
                rootFiles = this._folder.getFiles(new DirectoryInfo(rootFolderPath), option);

                foreach (FileInfo file in rootFiles)
                {
                    tempPicture = null;
                    picture = null;

                    combinePath = Path.Combine(destFolderPath, Path.GetFileNameWithoutExtension(file.Name) + ".jpg");

                    if (!File.Exists(combinePath))
                    {
                        using (fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            tempPicture = Image.FromStream(fs);
                            //picture = new Bitmap(tempPicture.Clone() as Image, size); // > CPU
                            picture = new Bitmap(tempPicture, size);                         
                            picture.Save(combinePath);

                            tempPicture.Dispose();
                            picture.Dispose();
                        }

                        if (this._themeForm != null)
                        {
                            this._pbPicture.Value += 1;
                        }
                    }
                }
            }
        }
    }
}
